package com.service.lotterySystem.controller;

import com.service.config.utils.JsonXMLUtils;
import com.service.config.utils.RedisUtil;
import com.service.config.utils.Result;
import com.service.config.utils.UserTokenManager;
import com.service.lotterySystem.dto.UserDataDto;
import com.service.lotterySystem.dto.UserDto;
import com.service.lotterySystem.dto.UserInfoAndDataDto;
import com.service.lotterySystem.service.UserDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static com.service.config.constant.Constant.*;

/**
 * @author ：hjl
 * @date ：Created in 2022/01/11 19:14
 * @description：
 */
@RestController
@RequestMapping("userDataController")
@Api(value = "用户资料接口", tags = "用户资料接口")
public class UserDataController {
    @Resource
    private UserDataService userDataService;
    @Resource
    private RedisUtil redisUtil;

    /**
     * 根据用户id查询用户资料
     * @param
     * @return
     */
    @GetMapping(value = "queryUserData",produces="application/json")
    @ApiOperation(value = "根据用户id查询用户资料")
    @ResponseBody
    public Result queryUserData(@RequestHeader(value = "cloud-Auth-Token",required = false) String cloudAuthToken) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken+userId)) {
                UserInfoAndDataDto userDataDto = userDataService.queryUserById(userId);
                return Result.ok().setData(userDataDto);
            }
            return Result.failure(USERNAME_Off_SITE,usernameOffSite);
        }
        return Result.failure(USERNAME_REPEAT,"用户名已存在，请重新输入");
    }

    /**
     * 增加用户资料
     * @param
     * @return
     */
    @PostMapping(value = "addUserData",produces="application/json")
    @ApiOperation(value = "增加用户资料(舍弃)")
    @ResponseBody
    public Result addUserData(@RequestHeader(value = "cloud-Auth-Token",required = false) String cloudAuthToken,
                             @RequestBody UserDataDto userDataDto) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken+userId)) {
                UserDto userDto = JsonXMLUtils.json2obj(redisUtil.getValue(cloudAuthToken + userId), UserDto.class);
                return userDataService.addUserData(userDataDto);
            }
            return Result.failure(USERNAME_Off_SITE,usernameOffSite);
        }
        return Result.failure(USERNAME_REPEAT,"用户未登录，请登录后重试");
    }

    /**
     * 修改用户资料
     * @param
     * @return
     */
    @PostMapping(value = "updateUserData",produces="application/json")
    @ApiOperation(value = "修改用户资料")
    @ResponseBody
    public Result updateUserData(@RequestHeader(value = "cloud-Auth-Token",required = false) String cloudAuthToken,
                                 @RequestBody UserDataDto userDataDto) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken+userId)) {
                userDataDto.setuId(userId);
                return userDataService.updateUserData(userDataDto);
            }
            return Result.failure(USERNAME_Off_SITE,usernameOffSite);
        }
        return Result.failure(USERNAME_REPEAT,"用户未登录，请登录后重试");
    }
}
